package com.zeng.generate.code;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.Test;

/**
 * 测试生成代码
 * @author Leo
 * @date 2017/12/18
 */
public class GeneratorServiceEntity {

    @Test
    public void generateCode() {
        String[] tableName = {"tb_rate","tb_rate_detail","tb_rate_type"};
        generateByTables(false, "com.esharp.hrss.salary", tableName);
    }

    @Test
    public void generateCode1() {
        String[] tableName = {"other_table"};
        generateByTables(false, "com.sharding.test", tableName);
    }

    /**
     * @param serviceNameStartWithI false: user -> UserService, 设置成true: user -> IUserService
     * @param packageName
     * @param tableNames
     */
    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {

        String mysql = "jdbc:mysql://192.168.10.129:3306/hrss";
        // String sqlserver = "jdbc:sqlserver://localhost:1433;Databasename=CCS";

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(mysql)                
                .setUsername("root")
                .setPassword("dksh@123")
                //.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        		.setDriverName("com.mysql.jdbc.Driver");


        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setService("/vm/serviceImpl.java");
        templateConfig.setServiceImpl("/vm/service.java");
        templateConfig.setController("/vm/controller.java");
        templateConfig.setXml(null);
        
        StrategyConfig strategyConfig = new StrategyConfig()
        	//.setSkipView(true)
        	.setTablePrefix("tb")
        	.setCapitalMode(true)
            .setEntityLombokModel(true)
            .setRestControllerStyle(true)
            .setNaming(NamingStrategy.underline_to_camel)
            .setColumnNaming(NamingStrategy.underline_to_camel)
            .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组 
        
        GlobalConfig config = new GlobalConfig()
        	.setActiveRecord(false)
            .setAuthor("Zeng")
            .setOutputDir("d:\\codeGen")
            .setFileOverride(true).setIdType(IdType.AUTO)
            .setDateType(DateType.ONLY_DATE);
        
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        
        PackageConfig packageConfig = new PackageConfig()
                .setParent(packageName)
                .setController("controller")
                .setEntity("entity");
        
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                //.setTemplate(templateConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}